package com.example.saifly.inPageRecord;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.BodyHistoryData.BodyHistoryBloodOxygenActivity;
import com.example.saifly.inPageRecord.BodyHistoryData.BodyHistoryBloodOxygenPressureActivity;
import com.example.saifly.inPageRecord.BodyHistoryData.BodyHistoryBloodPressureActivity;
import com.example.saifly.inPageRecord.BodyHistoryData.BodyHistoryCarbonDioxidePressureActivity;
import com.example.saifly.inPageRecord.BodyHistoryData.BodyHistoryTemperatureActivity;
import com.example.saifly.inPageRecord.RecordModel.BodyData;

import java.io.IOException;
import java.util.Date;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class BodyDataActivity extends Activity {
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_record_body_data;
    }

    private ImageView ivReturn;
    private RelativeLayout rlBodyTemperature,rlBloodPressure,rlBloodOxygen,rlBloodOxygenPressure,rlCarbonDioxidePressure;
    private TextView tvBodyTemperature,tvBloodPressure,tvBloodOxygen,tvBloodOxygenPressure,tvCarbonDioxidePressure;
    @Override
    protected void init()
    {
        super.init();

        tvBloodPressure=findViewById(R.id.bloodPressure_word_show);
        tvBloodOxygenPressure=findViewById(R.id.bloodOxygenPressure_word_show);
        tvBloodOxygen=findViewById(R.id.bloodOxygen_word_show);
        tvBodyTemperature=findViewById(R.id.temperature_word_show);
        tvCarbonDioxidePressure=findViewById(R.id.carbonDioxidePressure_word_show);

        ivReturn = findViewById(R.id.bodyData_return);
        rlBodyTemperature=findViewById(R.id.bodyData_temperature);
        rlBloodPressure=findViewById(R.id.bodyData_bloodPressure);
        rlBloodOxygen=findViewById(R.id.bodyData_bloodOxygen);
        rlBloodOxygenPressure=findViewById(R.id.bodyData_bloodOxygenPressure);
        rlCarbonDioxidePressure=findViewById(R.id.bodyData_carbonDioxidePressure);
        updateUi();
        doJump();

    }

    private void doJump()
    {
        ivReturn.setOnClickListener(view -> finish());
        rlBodyTemperature.setOnClickListener(v -> startActivity(new Intent(this, BodyHistoryTemperatureActivity.class)));
        rlBloodPressure.setOnClickListener(v -> startActivity(new Intent(this, BodyHistoryBloodPressureActivity.class)));
        rlBloodOxygen.setOnClickListener(v -> startActivity(new Intent(this, BodyHistoryBloodOxygenActivity.class)));
        rlBloodOxygenPressure.setOnClickListener(v -> startActivity(new Intent(this, BodyHistoryBloodOxygenPressureActivity.class)));
        rlCarbonDioxidePressure.setOnClickListener(v -> startActivity(new Intent(this, BodyHistoryCarbonDioxidePressureActivity.class)));
    }
    private void updateUi()
    {
        SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
        String phoneNum=preferences.getString("uid","default");

        Date date = new Date();
        new Thread(new Runnable() {
            @Override
            public void run() {
                BodyGetData(phoneNum,date);
            }
        }).start();

    }

    private interface ApiBodySetData
    {
        @Headers({"Content-Type:application/json"})
        @POST("/estimate/getBodyData")
        Call<ResponseBody> BodyGetData(@Body JSONObject jsonObject);
    }

    private void BodyGetData(String patient_phone, Date date)
    {
        Long dateLong = date.getTime();

        RetrofitHelper retrofitHelper=new RetrofitHelper();
        Retrofit retrofit=retrofitHelper.getRetrofit();


        ApiBodySetData apiBodySetData=retrofit.create(ApiBodySetData.class);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("patientPhone",patient_phone);
        jsonObject.put("date", dateLong);
        Log.d("getdate", "GetDate: "+jsonObject.toString());

        Call<ResponseBody> call = apiBodySetData.BodyGetData(jsonObject);
        final Response<ResponseBody> response;
        try {
            response = call.execute();
            if(response.body() != null){
                JSONObject result=JSON.parseObject(response.body().string());
                if(result.getInteger("code")!=null&&result.getInteger("code")==1){
                    //这里放正常流程操作
                    //  dailyQesFinish = (int) result.get("result");
                    JSONObject resultJson=result.getJSONObject("result");
                    String resultData=resultJson.getString("bodyData");
                    BodyData bodyData= JSON.parseObject(resultData, BodyData.class);

                    Log.d("getdata","result:"+bodyData.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String bloodPressure;
                            bloodPressure= bodyData.getLowBloodPressure().toString()+"mmHg"+'/'+bodyData.getHeightBloodPressure().toString()+"mmHg";

                            tvBodyTemperature.setText(bodyData.getBodyTemperature().toString()+"℃");
                            tvCarbonDioxidePressure.setText(bodyData.getCarbonPressure().toString()+"mmHg");
                            tvBloodOxygen.setText(bodyData.getBloodOxygen().toString()+"%");
                            tvBloodOxygenPressure.setText(bodyData.getBloodOxygenPressure().toString()+"mmHg");
                            tvBloodPressure.setText(bloodPressure);
                        }
                    });
                }
                else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(BodyDataActivity.this, "网络出现异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BodyDataActivity.this, "服务器在开小差儿", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }

    }

}
